package com.xbts.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.xbts.system.domain.CnCloudMarketSku;
import com.xbts.system.domain.dto.CnCloudMarketSkuDTO;
import com.xbts.system.domain.vo.CnCloudMarketUserVO;
import com.xbts.system.domain.vo.CnCloudMarketVO;
import com.xbts.system.service.ICnCloudMarketTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnCloudMarket;
import com.xbts.system.service.ICnCloudMarketService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 云市场Controller
 * @author xbts
 * @date 2023-08-22
 */
@Api("云市场")
@RestController
@RequestMapping("/system/market")
public class CnCloudMarketController extends BaseController {

    @Autowired
    private ICnCloudMarketService cnCloudMarketService;

    /**
     * 查询云市场列表
     */
    @ApiOperation("查询云市场列表")
    @PreAuthorize("@ss.hasPermi('system:market:list')")
    @GetMapping("/userBelongToList")
    public TableDataInfo userBelongToList(String searchValue) {
        startPage();
        List<CnCloudMarketUserVO> list = cnCloudMarketService.userBelongToList(searchValue);
        return getDataTable(list);
    }

    /**
     * 查询云市场列表
     */
    @ApiOperation("查询云市场列表")
//    @PreAuthorize("@ss.hasPermi('system:market:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnCloudMarket cnCloudMarket) {
        startPage(cnCloudMarket);
        List<CnCloudMarket> list = cnCloudMarketService.selectCnCloudMarketList(cnCloudMarket);
        return getDataTable(list);
    }

    /**
     * 导出云市场列表
     */
    @ApiOperation("导出云市场列表")
    @PreAuthorize("@ss.hasPermi('system:market:export')")
    @Log(title = "云市场", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody CnCloudMarket cnCloudMarket) {
        List<CnCloudMarket> list = cnCloudMarketService.selectCnCloudMarketList(cnCloudMarket);
        ExcelUtil<CnCloudMarket> util = new ExcelUtil<>(CnCloudMarket.class);
        util.exportExcel(response, list, "云市场数据");
    }

    /**
     * 获取云市场详细信息
     */
    @ApiOperation("获取云市场详细信息")
    @ApiImplicitParam(name = "id", value = "云市场ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:market:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(cnCloudMarketService.selectCnCloudMarketById(id));
    }  /**
     * 获取云市场详细信息
     */
    @ApiOperation("获取云市场详细信息")
    @ApiImplicitParam(name = "id", value = "云市场ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") String id) {
        return success(cnCloudMarketService.getById(id));
    }
    /**
     * 获取云市场第一级目录
     */
    @ApiOperation("获取云市场第一级目录")
    @GetMapping(value = "/getFirstDirectory/{marketId}")
    public AjaxResult getFirstDirectory(@PathVariable("marketId") String marketId) {
        return success(cnCloudMarketService.getFirstDirectory(marketId));
    }

    /**
     * 根据一级目录查询二级目录下所有的数据
     */
    @ApiOperation("根据一级目录查询二级目录下所有的数据")
    @PostMapping(value = "/getNextDirectory")
    public AjaxResult getNextDirectory(@RequestBody CnCloudMarketSku cnCloudMarketSku) {
        return success(cnCloudMarketService.getNextDirectory(cnCloudMarketSku));
    }

    /**
     * 根据一级目录和二级目录查询购买方式
     */
    @ApiOperation("根据一级目录和二级目录查询购买方式")
    @PostMapping(value = "/getBuyingMethod")
    public AjaxResult getPurchaseMethod(@RequestBody CnCloudMarketSku cnCloudMarketSku) {
        return success(cnCloudMarketService.getPurchaseMethod(cnCloudMarketSku));
    }


    /**
     * 云市场规格价格计算
     */
    @ApiOperation("云市场规格价格计算")
    @PostMapping(value = "/priceCalculation")
    public AjaxResult priceCalculation(@RequestBody CnCloudMarketSkuDTO cnCloudMarketSku) {
        return success(cnCloudMarketService.priceCalculation(cnCloudMarketSku));
    }



    /**
     * 新增云市场
     */
    @ApiOperation("新增云市场")
    @PreAuthorize("@ss.hasPermi('system:market:add')")
    @Log(title = "云市场", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnCloudMarketVO cnCloudMarket) {
        return toAjax(cnCloudMarketService.insertCnCloudMarket(cnCloudMarket));
    }

    /**
     * 修改云市场
     */
    @ApiOperation("修改云市场")
    @PreAuthorize("@ss.hasPermi('system:market:edit')")
    @Log(title = "云市场", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CnCloudMarketVO cnCloudMarket) {
        return toAjax(cnCloudMarketService.updateCnCloudMarket(cnCloudMarket));
    }

    /**
     * 删除云市场
     */
    @ApiOperation("删除云市场")
    @ApiImplicitParam(name = "ids", value = "云市场ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:market:remove')")
    @Log(title = "云市场", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> ids) {
        return toAjax(cnCloudMarketService.deleteCnCloudMarketByIds(ids));
    }

}
